/**
 * 
 */
package com.geekteam.geometrychess.utils;

import com.geekteam.geometrychess.room.entity.Player;
import com.geekteam.geometrychess.utils.Constants.Key;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

/**
 * @author MtRol
 *
 */
public class Utils {
	public static ISFSObject toSFSObject(User user){
		ISFSObject object = new SFSObject();
		object.putUtfString("name", user.getName());
		object.putInt("sfsId", user.getId());
		return object;
	}
	
	public static Player getPlayerFromSFSUser(User user) throws RuntimeException{
		Player player = new Player();
		if(user.containsVariable(Key.VARIABLE)){
			player.mSfsId = user.getId();
			player.mName = user.getName();
			player.mAvatar = user.getVariable(Key.VARIABLE).getSFSObjectValue().getUtfString(Key.AVATAR);
			player.mUserName = user.getVariable(Key.VARIABLE).getSFSObjectValue().getUtfString(Key.USER_NAME);
			player.mEmail = user.getVariable(Key.VARIABLE).getSFSObjectValue().getUtfString(Key.EMAIL);
			player.mBirthDay = user.getVariable(Key.VARIABLE).getSFSObjectValue().getLong(Key.BIRTHDAY);
			player.mGender = user.getVariable(Key.VARIABLE).getSFSObjectValue().getInt(Key.GENDER);
			player.mExp = user.getVariable(Key.VARIABLE).getSFSObjectValue().getDouble(Key.EXP);
			player.mMoney = user.getVariable(Key.VARIABLE).getSFSObjectValue().getDouble(Key.MONEY);
			player.mWin = user.getVariable(Key.VARIABLE).getSFSObjectValue().getLong(Key.WIN);
			player.mTotal = user.getVariable(Key.VARIABLE).getSFSObjectValue().getLong(Key.TOTAL);
			player.mLevel = user.getVariable(Key.VARIABLE).getSFSObjectValue().getInt(Key.LEVEL);
			player.mId = user.getVariable(Key.VARIABLE).getSFSObjectValue().getLong(Key.ID);
		}else{
			throw new RuntimeException("=============user dont have variable============\n" +
					"user:   "+user.getName());
		}
		return player;
	}
}
